package com.heima.Service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.Serivce.WemediaCilent;
import com.heima.model.Admin.Dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;

import static com.heima.common.exception.RabbitMQcode.WmExchangeName;
import static com.heima.common.exception.RabbitMQcode.WmSensitiveKey;

@Service
@Slf4j
public class AdminSensitiveService implements com.heima.Service.AdminSensitiveService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    WemediaCilent wemediaCilent;

//    删除敏感词
    @Override
    public ResponseResult SensitiveDel(Integer id) {
        try
        {
            //        配置ConfirmCallBack
            CorrelationData correlationData=SetCallback(id+"_del");

            String data=id+"_del";
            rabbitTemplate.convertAndSend(WmExchangeName,WmSensitiveKey,data,correlationData);
        }
        catch (Exception e)
        {
            return ResponseResult.errorResult(503,"服务出错");
        }

        return ResponseResult.okResult("Sensitive成功删除");
    }

//    查询敏感词
    @Override
    public ResponseResult GetSensitiveList(SensitiveDto sensitiveDto) {
       return  wemediaCilent.GetSensitiveList(sensitiveDto);
    }

    @Override
    public ResponseResult SAveSensitive(WmSensitive wmSensitive) {
        if(wmSensitive==null)
            return ResponseResult.errorResult(404,"数据不能为空");
        try
        {
            String id=wmSensitive.getSensitives()+"_save";
            CorrelationData correlationData=SetCallback(id);
            String data=JSON.toJSONString(wmSensitive)+"_save";
            rabbitTemplate.convertAndSend(WmExchangeName,WmSensitiveKey,data,correlationData);
        }
        catch (Exception e)
        {
            return ResponseResult.errorResult(503,"服务出错");
        }
        return ResponseResult.okResult("Sensitive成功删除");
    }

    @Override
    public ResponseResult UpdateSensitive(WmSensitive wmSensitive) {
        if(wmSensitive==null)
            return ResponseResult.errorResult(404,"数据不能为空");
        try
        {
            String id=wmSensitive.getId()+"_update";
            CorrelationData correlationData = SetCallback(id);

            String data=JSON.toJSONString(wmSensitive)+"_update";
            rabbitTemplate.convertAndSend(WmExchangeName,WmSensitiveKey,data,correlationData);
        }
        catch (Exception e)
        {
            return ResponseResult.errorResult(503,"服务出错");
        }
        return ResponseResult.okResult("Sensitive修改成功");
    }

    private CorrelationData SetCallback(String id)
    {
        //        配置ConfirmCallBack
        CorrelationData correlationData=new CorrelationData(id);
        correlationData.getFuture().addCallback(new SuccessCallback<CorrelationData.Confirm>() {
            @Override
            public void onSuccess(CorrelationData.Confirm confirm) {
                if(confirm.isAck())
                    log.info("消息发送成功，id:{}",correlationData.getId());

                else
                    log.error("消息发送到交换机失败，id:{},原因:{}",correlationData.getId(),confirm.getReason());
            }
        }, new FailureCallback() {
            @Override
            public void onFailure(Throwable throwable) {
                log.error("消息发送失败，id:{},愿因:{}",correlationData.getId(),throwable.getMessage());
            }
        });
        return correlationData;
    }
}
